package cn.sccl.dirms.security.controller;

import cn.sccl.dirms.security.service.RoleManager;
import com.fasterxml.jackson.annotation.JsonView;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.security.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 杨浩宇 on 2016-02-24.
 */
@RestController
@RequestMapping(value = "/dirms/security/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleManager roleManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Role> listAllRole() {
        return roleManager.listAll();
    }

    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public Role findRole(@PathVariable("name") String name) {
        return roleManager.findByName(name);
    }

    @RequestMapping(value = "/data/{id}", method = RequestMethod.GET)
    public List<Role> findUserData(@PathVariable("id") String id) {
        return roleManager.findRoleData(id);
    }

    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<Role> pageQueryRole(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return roleManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public PageResult<Role> pageQueryRole1(HttpServletRequest request) {
        return roleManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }


    /**
     * 保存数据
     *
     * @param role
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public Role saveRole(@RequestBody Role role) {
        roleManager.save(role);
        return role;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST,RequestMethod.GET})
    @JsonView(Object.class)
    public boolean deleteRole(@PathVariable("id") String id) {
        roleManager.delete(id);
        return true;
    }
}
